package com.juyu.miliao.authentication.service;


import com.juyu.miliao.authentication.dao.mysql.ConfigAppDao;
import com.juyu.miliao.common.constant.EnumType;
import com.juyu.miliao.common.domain.ConfigApp;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author jh
 * 2018/11/14
 */
@Service
@Slf4j
public class ConfigAppService {

    @Resource
    private ConfigAppDao configAppDao;

    /**
     * 获取应用信息
     *
     * @param appPackageId appPackageId
     * @param phoneSystem  phoneSystem
     * @return ConfigApp
     */
    public ConfigApp getConfigApp(String appPackageId, String phoneSystem) {
        ConfigApp configApp = configAppDao.getConfigApp(appPackageId, phoneSystem);
        if (configApp == null) {
            log.error("应用不存在,appPackageId={},phoneSystem={}", appPackageId, phoneSystem);
            throw new ConsumeRuntimeException("应用不存在");
        }

        return configApp;
    }

    /**
     * 获取应用分组
     *
     * @param appPackageId appPackageId
     * @param phoneSystem  phoneSystem
     * @return com.juyu.miliao.common.constant.PackageGroupConstant
     */
    public Integer getPackageGroup(String appPackageId, String phoneSystem) {
        ConfigApp configApp = getConfigApp(appPackageId, phoneSystem);
        return configApp.getPackageGroup();
    }

    /**
     * 获取新的应用分组 0-同城约聊 1-花蜜 2-粉包 -1-未知
     *
     * @param appPackageId appPackageId
     * @param phoneSystem  phoneSystem
     * @return com.juyu.miliao.common.constant.EnumType
     */
    public Integer getGroupType(String appPackageId, String phoneSystem) {
        ConfigApp configApp;
        try {
            configApp = getConfigApp(appPackageId, phoneSystem);
        } catch (Exception e) {
            return EnumType.PACKAGE_GROUP_UNKNOWN.state;
        }

        return configApp.getGroupType();
    }

}
